

function CommunicationController(contentController, servletPath) {
    var requestUrl = servletPath + '/pull';
    var xhr;

    
    this.start = function () {
        xhr = getTransport();
        performRequest();
    };


    var getTransport = function () {
        var methods = [
            function() {
                return new XMLHttpRequest();
            },
            function() {
                return new ActiveXObject('Msxml2.XMLHTTP');
            },
            function() {
                return new ActiveXObject('Microsoft.XMLHTTP');
            }
        ];

        var returnVal;
        for (var i = 0, len = methods.length; i < len; i++) {
            try {
                returnVal = methods[i]();
            } catch(e) {
                continue;
            }
            return returnVal;
        }
        
        throw new Error('Could not create an XHR object.');
    };




    var performRequest = function () {
        if (xhr) {
            xhr.open("GET", requestUrl + '?random=' + Math.random(), true);
            xhr.onreadystatechange = responseHandler;
            xhr.onerror = xhr.onabort = processResponse; // fixing firefox bug  http://www.nczonline.net/blog/2009/07/09/firefox-35firebug-xmlhttprequest-and-readystatechange-bug/
            xhr.send(null);
        }
    };

    var processResponse = function () {
        var responseText = xhr.responseText;
        if (responseText) {
            performRequest();
            contentController.addMessage(responseText);
        }
    }

    var responseHandler = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                processResponse();
            } else {
                performRequest();
            }
        }  
    };
}